<#include "../common/common.html"/> <@layout>
<script type="text/javascript">
    var url;
    /**
     * 打开新增用户的表单，录入用户信息
     */
    function newModel() {
        $('#dlg').dialog('setTitle', '新增模型').dialog('open');
        $('#myform').form('clear');
        url = '${ctx}/curdSet/save';
    }
    /**
     * 编辑选中的用户信息
     */
    function editModel() {
        $('#myform').form('clear');

        var row = $('#dt-grids').datagrid('getSelected');
        if (row) {
            $('#dlg').dialog('setTitle', '修改模型').dialog('open');
            $('#myform').form('load', row);
            $.post('${ctx}/curdSet/queryFields?tablename='+row.tablename, function(d) {
                $('#cmbQueryfields,#cmbListfields,#cmbCufields').combogrid({
                    multiple:true,
                    idField:'id',
                    textField:'text',
                    data:d,
                    columns:[[
                        {checkbox:true},
                        {field:'id',title:'字段名',width:100},
                        {field:'text',title:'中文名',width:120}
                    ]]
                });
                $('#cmbQueryfields').combogrid('setValues',row.queryfields.split(','));
                $('#cmbListfields').combogrid('setValues',row.listfields.split(','));
                $('#cmbCufields').combogrid('setValues',row.cufields.split(','));
            },'json');
            url = '${ctx}/curdSet/update/' + row.id;
        } else {
            $.messager.show({
                title : '提示',
                msg : '请先选择，再进行修改。',
                timeout:2000
            });
        }
    }

    function deleteModel() {
        var row = $('#dt-grids').datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认', '您确定要删除选中的记录吗?', function(r) {
                if (r) {
                    $.post('${ctx}/curdSet/delete/' + row.id, function(data) {
                        $.messager.show({
                            title : '提示',
                            msg : data,
                            timeout:2000
                        });
                        $('#dt-grids').datagrid('reload'); // reload the user data
                    },'text');
                }
            });
        }else {
            $.messager.show({
                title : '提示',
                msg : '请先选择模型，再进行删除。',
                timeout:2000
            });
        }
    }
    /**
     * 保存新增的用户数据，并对登录密码进行校验
     */
    function saveModel() {
        $('#queryfields').val($('#cmbQueryfields').combogrid('getValues').join(','));
        $('#listfields').val($('#cmbListfields').combogrid('getValues').join(','));
        $('#cufields').val($('#cmbCufields').combogrid('getValues').join(','));
        $('#myform').form('submit', {
            url : url,
            onSubmit : function() {
                return $(this).form('validate');
            },
            success : function(data) {
                $.messager.show({ // show error message
                    title : '提示',
                    msg : data,
                    timeout:2000
                });
                $('#dlg').dialog('close');
                $('#dt-grids').datagrid('reload');
            }
        });
    }

    function queryModel() {
        $('#dt-grids').datagrid('load', $.getQueryParam('queryTable'));
    }
    $(function(){
        $.post('${ctx}/curdSet/queryTables', function(d) {
            $('#q_tablename').combobox({
                data:d,
                valueField:'id',
                textField:'text'
            });
            $('#tablename').combobox({
                data:d,
                valueField:'id',
                textField:'text',
                onSelect: function(rec){
                    $.post('${ctx}/curdSet/queryFields?tablename='+rec.id, function(d) {
                        $('#cmbQueryfields,#cmbListfields,#cmbCufields').combogrid({
                            multiple:true,
                            idField:'id',
                            textField:'text',
                            data:d,
                            columns:[[
                                {checkbox:true},
                                {field:'id',title:'字段名',width:100},
                                {field:'text',title:'中文名',width:120}
                            ]]
                        });
                    },'json');
                }
            });
        },'json');
    });
</script>
<body class="easyui-layout">
	<div data-options="region:'north'" style="height: 35px; padding: 4px">
		<table id='queryTable' cellpadding="0" cellspacing="0"
			class="form-table" align='center'>
			<tr>
				<td style="width: 60px">表名：</td>
				<td style="width: 180px"><input id='q_tablename'
					name="search_EQ_tablename" class="query" style="width: 150px" /></td>
				<td colspan='2' align='center'><a href="#"
					class="easyui-linkbutton" iconCls="icon-search"
					onclick="queryModel()">查询</a></td>
			</tr>
		</table>
	</div>
	<div id='center' region="center" title='结果列表' border="false">
		<table id="dt-grids" class="easyui-datagrid"
			data-options="singleSelect:true,rownumbers:true,fit:true,pagination:true,toolbar:'#toolbar',url:'${ctx}/curdSet/query'">
			<thead>
				<tr>
					<th data-options="field:'tablename',width:150">表名</th>
					<th data-options="field:'queryfields',width:150">查询列组</th>
					<th data-options="field:'listfields',width:260">列表列组</th>
					<th data-options="field:'cufields',width:260">维护列组</th>
					<th data-options="field:'rmk',width:200">备注</th>
				</tr>
			</thead>
		</table>

		<div id="toolbar">
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"
				onclick="newModel()">新增</a> <a href="#" class="easyui-linkbutton"
				iconCls="icon-edit" plain="true" onclick="editModel()">编辑</a> <a
				href="#" class="easyui-linkbutton" iconCls="icon-remove"
				plain="true" onclick="deleteModel()">删除</a>
		</div>
	</div>
	<div id="dlg" class="easyui-dialog"
		style="top:60%;left:60%;width: 500px; height: 400px; padding: 10px" closed="true"
		modal="true" buttons="#dlg-buttons">
		<form id="myform" method="post">
			<input type="hidden" name="id" /> <input type="hidden"
				id='queryfields' name="queryfields" /> <input type="hidden"
				id='listfields' name="listfields" /> <input type="hidden"
				id='cufields' name="cufields" />
			<table cellpadding="0" cellspacing="0" class="form-table"
				align='center'>
				<tr>
					<td style="width: 60px">表名：</td>
					<td style="width: 180px"><input id='tablename'
						name="tablename" style="width: 325px" /></td>
				</tr>
				<tr>
					<td style="width: 60px">查询列组：</td>
					<td style="width: 180px"><input id='cmbQueryfields'
						style="width: 325px" /></td>
				</tr>
				<tr>
					<td style="width: 60px">列表列组：</td>
					<td style="width: 180px"><input id='cmbListfields'
						style="width: 325px" /></td>
				</tr>
				<tr>
					<td style="width: 60px">维护列组：</td>
					<td style="width: 180px"><input id='cmbCufields'
						style="width: 325px" /></td>
				</tr>
				<tr>
					<td>备注：</td>
					<td><textarea name="rmk" cols="50" rows="10"></textarea></td>
				</tr>
			</table>
		</form>
		<div id="dlg-buttons" style="text-align: center">
			<a href="#" class="easyui-linkbutton" iconCls="icon-save"
				onclick="saveModel()">保存</a> <a href="#" class="easyui-linkbutton"
				iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
		</div>
	</div>
</body>
</@layout>
